// pages/to_pay/to_pay.js
var util = require('../../utils/util.js');
const app = getApp()
Page({
  data: {
    id:'',
    price_num:"0.00",
    now_pay_index:0,
    data:[],
    pay_type:[
      {
        name: '冻结金额',
        icon: '/images/ye_pay.png',
        val:1
      },
      {
        name:'余额支付',
        icon:'/images/ye_pay.png',
        val:2
      },
      {
        name: '微信支付',
        icon: '/images/wx_pay.png',
        val:3
      },
      {
        name: '线下支付',
        icon: '/images/underline_pay.png',
        val:4
      }
    ]
  },
  onLoad:function(options){
    var id = options.id;
    var param = {
      id:id,
     
    };
    var that = this;
    wx.showLoading({})
    util.commonRequest('orderPrice', 'post', param, function (res) {
     
      wx.hideLoading()
      if (res.data.code == 200) {
        that.setData({
          data:res.data.data,
          price_num:res.data.data.price
        })
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });

    this.setData({
      id:id
    })
  },

  input_price(e){
    this.setData({
      price_num:e.detail.value
    })
  },
  select_pay_type(e){
    this.setData({
      now_pay_index:e.currentTarget.dataset.index
    })
  },

  pay_func(){
    var price = this.data.price_num;
    var pay_type = this.data.pay_type[this.data.now_pay_index].val;
    if(pay_type == 3){
      this.onlinePayment();
      return;
    }
    var param = {
      id:this.data.id,
      pay_type:pay_type,
      price:price
    };
  
    var that = this;
    wx.showLoading({})
    util.commonRequest('paymentOrder', 'post', param, function (res) {

      wx.hideLoading()
      if (res.data.code == 200) {
        wx.showToast({
          title: res.data.msg
        })

        // app.navTo('../shipper_waybill/waybill?type=-1');
        wx.redirectTo({
          url: '../shipper_waybill/waybill?type=-1',
        })
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
       
      }
    });
  },
  //线上支付
  onlinePayment:function()
  {
    var param = {
      id: this.data.id,
      total: this.data.price_num,
      body:'支付运费'
    };
    var that = this;
    wx.showLoading({})
    util.commonRequest('onlinePayment', 'post', param, function (res) {
      
      wx.hideLoading()
      if (res.data.code == 200) {
        wx.requestPayment({
          'timeStamp': '' + res.data.data.timeStamp + '',
          'nonceStr': '' + res.data.data.nonceStr + '',
          'package': '' + res.data.data.package + '',
          'signType': 'MD5',
          'paySign': '' + res.data.data.paySign + '',
          success: function (res) {
            console.log(res)
            wx.showToast({
              title: '支付成功',
              icon: 'loading'
            })
            // app.navTo('../shipper_waybill/waybill?type=-1');
            wx.redirectTo({
              url: '../shipper_waybill/waybill?type=-1',
            })
          },
          fail: function (res) {
            wx.showToast({
              title: '支付失败',
              icon: 'loading'
            })
          }
        })
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  }
})